package com.school.hjt.schoolclasscard.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.school.hjt.schoolclasscard.R;
import com.school.hjt.schoolclasscard.db.CardStudent;

import java.util.List;

/**
 * Created by xqx on 2017/10/25.
 * 照片墙
 */

public class StudentnoToAdapter extends BaseAdapter {
    private Context mContext;
    private List<CardStudent> list;

    public StudentnoToAdapter(Context mContext, List<CardStudent> list) {
        this.mContext = mContext;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHander viewHander;
        if (convertView==null){
            viewHander=new ViewHander();
            convertView=View.inflate(mContext, R.layout.banjis_item,null);
            viewHander.iv= convertView.findViewById(R.id.iv_banjir);
            viewHander.tv_name=convertView.findViewById(R.id.tv_name);
            convertView.setTag(viewHander);

        }else{
            viewHander= (ViewHander) convertView.getTag();
        }
        CardStudent classKaoQinDetail = list.get(position);
        viewHander.tv_name.setText(classKaoQinDetail.getStudent_name());
        if (classKaoQinDetail.getStudent_url().equals("无头像")){
//            viewHander.iv.setImageResource(R.mipmap.student);
        }else {
            Glide.with(mContext).load(classKaoQinDetail.getStudent_url()).error(R.mipmap.student).listener(new RequestListener<String, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    notifyDataSetChanged();
                    return false;
                }
            }).into(viewHander.iv);
        }
        return convertView;
    }
    class ViewHander{
        private ImageView iv;
        private TextView tv_name;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }
}
